package com.example.student.controller;

import com.example.student.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import com.example.student.utils.ResultBody;
import com.example.student.config.PageParam;
import com.example.student.service.StudentService;
import com.example.student.entity.Student;


/**
 * 学生 控制器
 * @author lx
 */
@RestController
@RequestMapping(value = "/student")
public class StudentController {
       @Resource
       public StudentService service;
       @Resource
       private JwtUtils jwtUtils;

       /**
        * 新增
        * @param student
        */
       @PostMapping("/add")
       public ResultBody add(@RequestBody Student student){
            this.service.add(student);
            return ResultBody.success("添加成功");
        }

       /**
        * 修改
        * @param student
        */
       @PostMapping("/update")
       public ResultBody update(@RequestBody Student student){
            this.service.update(student);
            return ResultBody.success("修改成功");
        }

        /**
         * 查找
         * @param id
         */
        @GetMapping("/getOne")
        public ResultBody getOne(String id){
            Student obj=this.service.getOne(id);
            if(obj!=null){
                return ResultBody.success(obj);
            }else{
                return ResultBody.error("未获取到数据!");
            }
        }

        /**
        * 删除单条
        * @param id
        */
        @PostMapping("/delete")
        public ResultBody delete(@RequestBody String  id){
            this.service.delete(id);
            return ResultBody.success("删除成功");
        }

        /**
         * 删除多条
         * @param ids
         */
        @PostMapping("/deleteByIds")
        public ResultBody deleteByIds(@RequestBody String[]  ids){
            this.service.deleteByIds(ids);
            return ResultBody.success("删除成功");
        }

        /**
         * 列表查询
         * @param pageParam
         * @return
         */
        @PostMapping(value = "/page")
        public ResultBody page(@RequestBody PageParam pageParam){
            return ResultBody.success(this.service.page(pageParam));
        }

        /**
         * 密码重置
         * @param ids
         * @author lx
         * @date 2021/1/27 16:54
         * @return
         */
        @PostMapping(value = "/restPassword")
        public ResultBody restPassword(@RequestBody String[]  ids){
            boolean check = this.service.restPassword(ids);
            if(!check){
                return ResultBody.error("密码重置失败，请联系管理员");
            }
            return ResultBody.success("密码成功重置，重置后密码为：123456");
        }

        /**
         * 图表统计接口
         */
        @GetMapping("/report")
        public ResultBody report(){
            return ResultBody.success(this.service.report());
        }

        /**
         * 获取学生信息
         * @param token
         * @return
         */
       @GetMapping(value = "/getStudentInfo")
       public ResultBody info(String token) {
           if (token == null || "".equals(token)) {
               return ResultBody.error("登录已失效！");
           }
           Claims claims = jwtUtils.parseJwt(token);
           String id = String.valueOf(claims.get("id"));
           return ResultBody.success(service.getOne(id));
       }
}
